/**
 * 
 */
package com.cp16.tss.handler;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.cp16.tss.bo.ServiceBO;
import com.cp16.tss.common.Constants;
import com.cp16.tss.dto.Service;
import com.cp16.tss.form.ServiceAddForm;
import com.cp16.tss.form.ServiceEditForm;
import com.cp16.tss.form.ServiceForm;
import com.cp16.tss.validation.ServiceValidation;


/**
 * @author ADMIN
 *
 */
public class ServiceHandler {

	private ServiceBO serviceBO = new ServiceBO();
	
	@SuppressWarnings("unchecked")
	public void loadTable(ServiceForm serviceForm) {

		@SuppressWarnings("rawtypes")
		Vector rowHeader = new Vector();
		rowHeader.add("ID");
		rowHeader.add("Name");
		rowHeader.add("Price");
		rowHeader.add("Date");
		
		DefaultTableModel model = new DefaultTableModel(rowHeader, 0);

		List<Service> serviceList;
		try {
			serviceList = serviceBO.getAllService();
			if (serviceList != null) {
				for (Service service : serviceList) {
					@SuppressWarnings("rawtypes")
					Vector rowData = new Vector();
					rowData.add(service.getServiceId());
					rowData.add(service.getServiceName());
					rowData.add(service.getServicePrice());
					rowData.add(service.getDateOfQuotation());
					model.addRow(rowData);
				}
			}
			serviceForm.getTblResultSearch().setModel(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void loadValueComboBoxServiceId(ServiceForm serviceForm) {
		@SuppressWarnings("rawtypes")
		DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
		List<Service> serviceList;
		try {
			serviceList = serviceBO.getAllService();
			if (serviceList != null) {
				for (Service service : serviceList) {
					comboBoxModel.addElement(service.getServiceId());
				}
			}
			serviceForm.getCbServiceId().setModel(comboBoxModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void loadEditServiceForm(ServiceForm serviceForm) {
		int selectColumnsNo = serviceForm.getTblResultSearch().getSelectedRowCount();
		if (selectColumnsNo > 0) {
			int selectRow = serviceForm.getTblResultSearch().getSelectedRow();
			int rowIndex = serviceForm.getTblResultSearch().convertRowIndexToModel(selectRow);

			String ID = (String) serviceForm.getTblResultSearch().getValueAt(rowIndex, 0);
			String name = (String) serviceForm.getTblResultSearch().getValueAt(rowIndex, 1);
			int price = (Integer) serviceForm.getTblResultSearch().getValueAt(rowIndex, 2);
			Date date = (Date) serviceForm.getTblResultSearch().getValueAt(rowIndex, 3);

			ServiceEditForm serviceEditForm = new ServiceEditForm();
			serviceEditForm.getTxtServiceId().setText(ID);
			serviceEditForm.getTxtServiceId().setEditable(false);
			serviceEditForm.getTxtServiceName().setText(name);
			serviceEditForm.getTxtServicePrice().setText(String.valueOf(price));
			serviceEditForm.getTxtDateOfQuotation().setDate(date);
			
			serviceEditForm.setVisible(true);

		}
	}

	public void deleteServiceFromTable(ServiceForm serviceForm) {
		int selectColumnsNo = serviceForm.getTblResultSearch().getSelectedRowCount();
		int[] rowColumnsSelected = serviceForm.getTblResultSearch().getSelectedRows();
		if (selectColumnsNo > 0) {
			int click = JOptionPane.showConfirmDialog(serviceForm,
					"Do you want to delete selected service?",
					"Confirm delete service", JOptionPane.YES_NO_OPTION);

			if (click == JOptionPane.YES_OPTION) {
				int rowIndex;
				String cellSelect;
				Service service;
				for (int i = 0; i < selectColumnsNo; i++) {
					rowIndex = serviceForm.getTblResultSearch().convertRowIndexToModel(rowColumnsSelected[i]) - i;
					cellSelect = (String) serviceForm.getTblResultSearch().getValueAt(rowIndex, 0);
					try {
						service = serviceBO.getServiceById(cellSelect);
						serviceBO.deleteService(service);
						((DefaultTableModel) serviceForm.getTblResultSearch().getModel()).removeRow(rowIndex);
						serviceForm.getTblResultSearch().repaint();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void displayServiceNameWhenSelectId(ServiceForm serviceForm) {
		String serviceId = serviceForm.getCbServiceId().getSelectedItem().toString();
		try {
			String serviceName = serviceBO.getServiceById(serviceId).getServiceName();
			serviceForm.getTxtServiceName().setText(serviceName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean addNewService(ServiceAddForm serviceAddForm) {
		boolean flag = false;
		if(ServiceValidation.validateServiceAddForm(serviceAddForm)){
			String serviceId = serviceAddForm.getTxtServiceId().getText();
			try {
				if(serviceBO.checkServiceExist(serviceId)){
					serviceAddForm.getLblAddNewService().setText("Service ID is existed");
					flag = false;
				} else {
					String serviceName = serviceAddForm.getTxtServiceName().getText();
					String servicePrice = serviceAddForm.getTxtServicePrice().getText();
					Date serviceDateOfQuotation = serviceAddForm.getTxtDateOfQuotation().getDate();
					
					Service service = new Service();
					service.setServiceId(serviceId);
					service.setServiceName(serviceName);
					service.setServicePrice(Integer.valueOf(servicePrice));
					if(serviceDateOfQuotation!= null){
						service.setDateOfQuotation(serviceDateOfQuotation);
					}
					if(serviceBO.addService(service)){
						serviceAddForm.getLblAddNewService().setText("Add new service successfull");
						flag = true;
					} else{
						flag = false;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}

	public void editService(ServiceEditForm serviceEditForm) {
		int click = JOptionPane.showConfirmDialog(serviceEditForm,
				"are you sure edit this service?", "Edit a service",
				JOptionPane.YES_NO_OPTION);
		if (click == JOptionPane.YES_OPTION) {
			try {
			Service service = new Service();
			service.setServiceId(serviceEditForm.getTxtServiceId().getText());
			service.setServiceName(serviceEditForm.getTxtServiceName().getText());
			service.setServicePrice(Integer.valueOf(serviceEditForm.getTxtServicePrice().getText()));
			service.setDateOfQuotation(serviceEditForm.getTxtDateOfQuotation().getDate());
				serviceBO.updateService(service);
				JOptionPane.showMessageDialog(serviceEditForm, "Edit successfull");
				serviceEditForm.setVisible(false);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(serviceEditForm,
						"Edit fail " + e.getMessage());
				e.printStackTrace();
			}
		}
	}


	public void doClearServiceAddForm(ServiceAddForm serviceAddForm) {
		serviceAddForm.getLblAddNewService().setText("Add new a service");
		serviceAddForm.getTxtServiceId().setText(Constants.BLANK);
		serviceAddForm.getLblIdReq().setText(Constants.BLANK);
		serviceAddForm.getTxtServiceName().setText(Constants.BLANK);
		serviceAddForm.getLblServiceNameReq().setText(Constants.BLANK);
		serviceAddForm.getTxtServicePrice().setText(Constants.BLANK);
		serviceAddForm.getLblPriceReq().setText(Constants.BLANK);
		serviceAddForm.getTxtDateOfQuotation().setDate(null);
		
	}
	public List<Service> getAllService(){
		try {
			return serviceBO.getAllService();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
